using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Forms;
using SmartBoard.Background.BoardElement;
using SmartBoard.Background.Commands;
using SmartBoard.BackGround.Data;
using SmartBoard.Background.Dialogs;
using SmartBoard.Core.Command;
using SmartBoard.Core.Data;
using SmartBoard.Core.Element;
using SmartBoard.Background.Serialization;
using System.Windows.Media;
using System;
using System.Collections;
using SmartBoard.Core.Networking.Client;
using SmartBoard.Core.Server;
using System.IO;

namespace SmartBoard.BackGround.BoardElement
{
    public class BackGroundBoardElement : Canvas,IGraphicalBoardElement
    {
        #region Private Fields
        private readonly IBoardElementStreamManager _streamManager;
        private readonly IBoardElementDataManager _dataManager;
        private readonly BackGroundNetworkingModule _networkingModule;
        private readonly BoardCommandManager _boardCommandManager;
        #endregion

        #region Ctor
        public BackGroundBoardElement()
        {
            var versionConverter = new DataVersionConverter();
            var dataBroker = new BackgroundDataBroker(this);
            var serializationStrategyProvider = new SerializationStrategyProvider();

            _dataManager = new BackgroundDataManager(versionConverter, dataBroker);
            _streamManager = new BackgroundStreamManager(serializationStrategyProvider, _dataManager);

            _networkingModule = new BackGroundNetworkingModule(this);
            _boardCommandManager = new BoardCommandManager(this);
            this.Background = Brushes.WhiteSmoke;
        }
        #endregion

        #region Implementation of IBoardElement

        public bool IsInteractiveComponent
        {
            get { return false; }
        }

        public bool IsActive
        {
            get { return false; }
            set { throw new NotSupportedException("Background element can not be active."); }
        }

        public IBoardElementStreamManager StreamManager
        {
            get { return _streamManager; }
        }

        public IBoardElementDataManager DataManager
        {
            get { return _dataManager; }
        }

        public INetworkServiceModule NetworkServiceModule
        {
            get { return _networkingModule; }
        }

        public IBoardCommandManager CommandManager
        {
            get { return _boardCommandManager; }
        }

        public object BaseUI
        {
            get { return this; }
        }

        #endregion

        public void ChangeBackgroundColor()
        {
            var dialog = (IChangeBackgroundDialog) DialogManager.Instance.GetDialog(typeof (IChangeBackgroundDialog));
            dialog.SelectedColor = ((SolidColorBrush) this.Background).Color;
            if(dialog.ShowModal() == DialogResult.OK)
            {
                this.Background = new SolidColorBrush(dialog.SelectedColor);
                using (var stream = new MemoryStream())
                {
                    _streamManager.WriteToDataStream(stream);
                    var message = new ServerMessage(_networkingModule.ModuleId, 1, stream.ToArray());
                    _networkingModule.WriteMessage(message);
                }
            }
        }
    }
}